<?php

namespace App\Models;

use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jiaxincui\ClosureTable\Traits\ClosureTable;
use DB;

class Folder extends Base
{
    use ClosureTable;

    protected $prefix = 'sr_';

    // 关联表名，默认'Model类名+_closure',如'menu_closure'
    protected $closureTable = 'folder_closure';

    // ancestor列名,默认'ancestor'
    protected $ancestorColumn = 'ancestor';

    // descendant列名,默认'descendant'
    protected $descendantColumn = 'descendant';

    // distance列名,默认'distance'
    protected $distanceColumn = 'distance';

    // parent列名,默认'parent'
    protected $parentColumn = 'parent';

    protected $hidden = ['deleted_at','created_at','updated_at'];
    // ,'parent','ancestor','descendant','distance'

    use SoftDeletes;

    public function edit_data($id,$data){
        if(array_key_exists('id',$data)) unset($data['id']);
        $result = $this->where('id',$id)->update($data);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }

    public function delData($id_arr){
        DB::beginTransaction();
        try{
            $result = $this->whereIn('id',$id_arr)->delete();
            Article::whereIn('folder_id',$id_arr)->delete();
            DB::commit();
        }catch(Exception $e){
            DB::rollBack();
        }
        return $result;
    }

}
